class UserContext {
  static SessionInfo _sessionInfo;
  static UserInfo _userInfo;

  ///
  /// 设置当前会话信息
  ///
  static void setSession(var jsonObjectData) {
    String accessToken = jsonObjectData["accessToken"];
    String refershToken = jsonObjectData["refershToken"];
    String tokenType = jsonObjectData["tokenType"];
    int expireIn = jsonObjectData["expireIn"];
    var jti = jsonObjectData["jti"];

    _sessionInfo = new SessionInfo(
      accessToken: accessToken,
      refershToken: refershToken,
      tokenType: tokenType,
      expireIn: expireIn,
      jti: jti,
    );
  }

  ///
  /// 设置当前用户信息
  ///
  static void setUserInfo(var jsonObjectData) {
    int id = jsonObjectData["id"];
    String account = jsonObjectData["account"];
    String name = jsonObjectData["name"];
    int accountType = jsonObjectData["accountType"];
    var avatar = jsonObjectData["avatar"];

    _userInfo = new UserInfo(
      id: id,
      account: account,
      name: name,
      accountType: accountType,
      avatar: avatar,
    );
  }

  ///
  /// 获取当前账号的accessToken
  ///
  static String getCurrentAccessToken() {
    String result = "";
    if (_sessionInfo != null) {
      result = _sessionInfo.accessToken;
    }
    return result;
  }

  ///
  /// 获取当前账号的refershToken
  ///
  static String getCurrentRefershToken() {
    String result = "";
    if (_sessionInfo != null) {
      result = _sessionInfo.refershToken;
    }
    return result;
  }

  ///
  /// 获取当前账号的tokenType
  ///
  static String getCurrentTokenType() {
    String result = "";
    if (_sessionInfo != null) {
      result = _sessionInfo.tokenType;
    }
    return result;
  }

  ///
  /// 获取当前账号的expireIn
  ///
  static int getCurrentExpireIn() {
    int result = 0;
    if (_sessionInfo != null) {
      result = _sessionInfo.expireIn;
    }
    return result;
  }

  ///
  /// 判断accessToken是否为空
  ///
  static bool isAccessTokenEmpty() {
    return _sessionInfo == null ||
        _sessionInfo.accessToken == null ||
        _sessionInfo.accessToken.isEmpty;
  }

  ///
  /// 判断accessToken是否非空
  ///
  static bool isAccessTokenNotEmpty() {
    return !isAccessTokenEmpty();
  }

  ///
  /// 获取当前用户的认证信息，格式："$tokenType$accessToken"
  ///
  static String buildAuthorization() {
    String result = "";
    if (isAccessTokenNotEmpty()) {
      result = "${_sessionInfo.tokenType}${_sessionInfo.accessToken}";
    }
    return result;
  }

  ///
  /// 获取当前用户账号
  ///
  static String getCurrentUserAccount() {
    String result = "";
    if (_userInfo != null) {
      result = _userInfo.account;
    }
    return result;
  }

  ///
  /// 获取当前用户名称
  ///
  static String getCurrentUserName() {
    String result = "游客";
    if (_userInfo != null &&
        _userInfo.name != null &&
        _userInfo.name.isNotEmpty) {
      result = _userInfo.name;
    }
    return result;
  }

  ///
  /// 获取当前用户头像
  ///
  static String getCurrentUserAvatar() {
    String result = "";
    if (_userInfo != null) {
      result = _userInfo.avatar;
    }
    return result;
  }

  ///
  /// 判断用户账号是否为空
  ///
  static bool isUserAccountEmpty() {
    return _userInfo == null ||
        _userInfo.account == null ||
        _userInfo.account.isEmpty;
  }

  ///
  /// 判断用户账号是否非空
  ///
  static bool isUserAccountNotEmpty() {
    return !isUserAccountEmpty();
  }
}

///
/// 会话信息
///
class SessionInfo {
  String accessToken = "";
  String refershToken = "";
  String tokenType = "";
  int expireIn = 0;
  var jti;

  SessionInfo({
    this.accessToken,
    this.refershToken,
    this.tokenType,
    this.expireIn,
    this.jti,
  });
}

///
/// 用户信息
///
class UserInfo {
  int id = 0;
  String account = "";
  String name = "";
  int accountType = 0;
  String avatar = "";

  UserInfo({
    this.id,
    this.account,
    this.name,
    this.accountType,
    this.avatar,
  });
}
